package wanbang.dao.Imp;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import wanbang.dao.BookDao;
import wanbang.entities.Book;
@Repository
public class BookDaoImp implements BookDao {
	@Autowired
	private JdbcTemplate jdbcTemplate;
	@Override
	public List<Book> selectAllBook() {
		// TODO Auto-generated method stub
		String sql = "select book_id,book_name,book_stock,book_price from book ";
		RowMapper<Book> rowMapper = new BeanPropertyRowMapper<>(Book.class);
		List<Book> books = jdbcTemplate.query(sql, rowMapper);
		return books;
	}
	@Override
	public Double selectBookPriceByBookId(Integer bookId) {
		// TODO Auto-generated method stub
		String sql = "select book_price from book where book_id = ?";
		return jdbcTemplate.queryForObject(sql,  Double.class,bookId);
	}
	@Override
	public void updateBookStock(Integer bookId) {
		// TODO Auto-generated method stub
		String sql = "update book set book_stock = book_stock-1 where book_id = ?";
		jdbcTemplate.update(sql, bookId);
		
	}
	@Override
	public Integer selecetBookStockByBookId(Integer bookId) {
		// TODO Auto-generated method stub
		String sql = "select book_stock from book where book_id = ?";
		return this.jdbcTemplate.queryForObject(sql, Integer.class, bookId);
	}
	@Override
	public Book selectBybookId(Integer bookId) {
		// TODO Auto-generated method stub
		String sql = "select book_name from book where book_id = ?";
		RowMapper<Book> rowMapper = new BeanPropertyRowMapper<>(Book.class);
		return this.jdbcTemplate.queryForObject(sql, rowMapper, bookId);
	}
	@Override
	public List<Book> selectBookByLimit(Integer currentPage, Integer numberPage) {
		// TODO Auto-generated method stub
		String sql = "select book_id,book_name,book_stock,book_price from book limit ?,? ";
		RowMapper<Book> rowMapper = new BeanPropertyRowMapper<>(Book.class);
		List<Book> books = jdbcTemplate.query(sql, rowMapper,(currentPage-1)*numberPage,numberPage);
		return books;
	}
	@Override
	public Integer selectCountAllNumber() {
		// TODO Auto-generated method stub
		String sql = "select count(*) from book";
		return this.jdbcTemplate.queryForObject(sql, Integer.class);
	}


}
